import axios from 'axios'
import { Toast } from 'vant';
import 'vant/es/toast/style';
import store from '@/store/index'
import router from '@/router'
export default {
    common: {
        method: 'GET',
        data: {},
        params: {},
        headers:{}
    },
    $axios(option = {}) {

        Toast.loading({
            message: '加载中...',
            forbidClick: true,
        });
        option.method = option.method || this.common.method
        option.data = option.data || this.common.data
        option.params = option.params || this.common.params
        option.headers = option.headers || this.common.headers

        // 是否登录状态
        if(option.headers.token){
            // 代表需要验证token
            option.headers.token=store.state.user.token
            // 如果用户没有登录,跳去登录页
            if(!option.headers.token){
                Toast.clear()
                return router.push({path:'/login'}) 
            }
        }



        return axios(option).then((v) => {
            // let data = v.data.data;
            // //如果token过期，重新登录
            // if( data.code == 1000 ){
            //     Toast.clear()
            //     return router.push('/login');
            // }

            return new Promise((res, rej) => {
                if (v) {
                    
                    setTimeout(() => {
                        Toast.clear()
                    }, 500);
                    res(v)
                } else {
                   
                    setTimeout(() => {
                        Toast.clear()
                    }, 500);
                    rej()
                }
            })
        })
    }

}